package com.lsz.flight_system.controller;

import com.lsz.flight_system.entity.BuyTicketsMessage;
import com.lsz.flight_system.entity.PlaneMessage;
import com.lsz.flight_system.entity.UserMessage;
import com.lsz.flight_system.service.BuyTicketsMessageService;
import com.lsz.flight_system.service.PlaneMessageService;
import com.lsz.flight_system.service.UserMessageService;
import com.lsz.flight_system.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/planeMessage")
public class PlaneMessageController {

    @Autowired
    PlaneMessageService planeMessageService;

    @Autowired
    BuyTicketsMessageService buyTicketsMessageService;

    @Autowired
    UserMessageService userMessageService;

    @ResponseBody
    @GetMapping("/planeMessages")
    public Result planeMessages(@RequestParam(value = "planeId",required = false) String planeId,
                                @RequestParam(value = "origin",required = false) String origin,
                                @RequestParam(value = "destination",required = false) String destination,
                                @RequestParam(value = "startTime",required = false) String startTime){
        Map<String,Object> queryMap = new HashedMap<>();

        queryMap.put("planeId",planeId);
        queryMap.put("origin",origin);
        queryMap.put("destination",destination);
        queryMap.put("startTime",startTime);

//        System.out.println(queryMap);

        List<PlaneMessage> planeMessageList = planeMessageService.findAllPlaceMessage(queryMap);

//        log.info(planeMessageList.toString());

        return Result.ok(planeMessageList);
    }

//    @GetMapping("/findByPlaneId/{planeId}")
//    public Result findPlaneMessageByPlaneId(@PathVariable String planeId){
//        if (planeId == null){
//            return Result.fail("输入为空");
//        }
//        PlaneMessage planeMessage = planeMessageService.findByPlaneId(planeId);
//        if (planeMessage == null){
//            return Result.fail("无所输入的飞机信息");
//        }
//        return Result.ok(planeMessage);
//    }

    //用户购票，保存用户购票信息
    @PostMapping("/buyTickets/{planeId}")
    public Result userBuyTickets(@PathVariable String planeId, HttpSession session){
        UserMessage userMessage = (UserMessage) session.getAttribute("USER_MESSAGE");
        if (userMessage==null){
            return Result.fail("请登录后进行购票");
        }

        PlaneMessage planeMessage = planeMessageService.findByPlaneId(planeId);
        UserMessage user = userMessageService.getUserMessageByUsername(userMessage.getUsername());
        float balance = user.getBalance() - planeMessage.getTicketPrice();

        if (balance<0){return Result.fail("余额不足");}

        if (!user.getPermissions().equals("国外")&&planeMessage.getPlaneRoute().equals("国外")){
            return Result.fail("购票权限不足，无法出国");
        }

        boolean message = planeMessageService.updateRemainTicket(planeId);
        if (!message){
            return Result.fail("当前无票或已经停售");
        }

        if (!buyTicketsMessageService.updateUserBalance(userMessage.getUsername(), balance)){
            return Result.fail("扣费异常");
        }

        BuyTicketsMessage buyTicketsMessage = buyTicketsMessageService.getBuyTicketsMessage(userMessage,planeMessage);
        if (buyTicketsMessageService.addBuyTicketsMessage(buyTicketsMessage)){
            session.setAttribute("USER_MESSAGE",user);
            return Result.ok("购票成功");
        }
        return Result.fail("购票异常");
    }

    @PostMapping("/changeTickets/{planeId}")
    public Result userChangeTickets(@PathVariable String planeId,HttpSession session){
        UserMessage userMessage = (UserMessage) session.getAttribute("USER_MESSAGE");
        if (userMessage == null){
            return Result.fail("请登录后进行操作");
        }
        UserMessage message = userMessageService.getUserMessageByUsername(userMessage.getUsername());
        BuyTicketsMessage buyTicketsMessage = (BuyTicketsMessage) session.getAttribute("CHANGE_TICKETS");
        if (buyTicketsMessage==null){
            return Result.fail("未选中所换购的票");
        }
        PlaneMessage ticketMessage = planeMessageService.findByPlaneId(buyTicketsMessage.getPlaneId());
        PlaneMessage planeMessage = planeMessageService.findByPlaneId(planeId);
        if (!planeMessage.getPlaneState().equals("售票")){
            return Result.fail("当前无票或已停售");
        }
        if (message.getBalance() + ticketMessage.getTicketPrice() < planeMessage.getTicketPrice()){
            return Result.fail("金额不足，无法换票");
        }
        if (!message.getPermissions().equals("国外")&&planeMessage.getPlaneRoute().equals("国外")){
            return Result.fail("购票权限不足，无法出国");
        }
        BuyTicketsMessage ticketsMessage = planeMessageService.userChangeTickets(buyTicketsMessage.getId(),planeId);
        if (ticketsMessage.getTicketState().equals("换购")){
            float money = 0;
            session.setAttribute("CHANGE_TICKETS",null);
            if (ticketMessage.getTicketPrice()>=planeMessage.getTicketPrice()){
                money = ticketMessage.getTicketPrice() - planeMessage.getTicketPrice();
                return Result.ok("换购成功，已退回"+money+"元");
            }
            else {
                money = planeMessage.getTicketPrice() - ticketMessage.getTicketPrice();
                return Result.ok("换购成功，已支付"+money+"元");
            }
        }
        return Result.fail("换购机票异常");
    }
    @PostMapping("/getUpdatePlaneMessage")
    public Result getUpdatePlaneMessage(HttpSession session){
        BuyTicketsMessage buyTicketsMessage = (BuyTicketsMessage) session.getAttribute("CHANGE_TICKETS");
        if (buyTicketsMessage==null){
            return Result.fail("未选择换购的机票");
        }
        PlaneMessage planeMessage = planeMessageService.findByPlaneId(buyTicketsMessage.getPlaneId());
        return Result.ok(planeMessage);
    }
}
